define(["jquery"],function(jq){
  console.log("number utils init...");
  jq.numberUtils = (function() {
    /**
     * 数字工具类.
     * 
     * @constructor .
     */
    function NumberUtils() {
    }

    /**
     * 默认配置. $-999,000.000999
     */
    NumberUtils.DEFAULTS = {

    };

    var _find_separator = function(fmt) {
      var res = "";
      var flag = false;
      for (var i = 0; i < fmt.length; i++) {
        var cur = fmt.charAt(i);
        if (!(cur == '0' || cur == '9')) {
          flag = true;
          res += cur;
        } else if (flag) {
          break;
        }
      }
      return res;
    };

    NumberUtils.format = function(num, format) {

      if (!format) {
        format = "0";
      }

      var str = format;
      var baseStr = new Number(num).toString();
      var idxFormatPoint = str.indexOf(".");
      var idxBasePoint = baseStr.indexOf(".");

      if (idxFormatPoint < 0) {
        idxFormatPoint = str.length;
      }

      if (idxBasePoint < 0) {
        idxBasePoint = baseStr.length;
      }

      var fmtWhole = format.substr(0, idxFormatPoint);
      var fmtFractional = format.substr(idxFormatPoint);
      var strWhole = baseStr.substr(0, idxBasePoint);
      var strFractional = baseStr.substr(idxBasePoint + 1);

      if (fmtWhole == "") {// 支持.0格式写法
        fmtWhole = "0";
      }

      var res = "";
      // 处理整数部分
      var idx0_fmt = fmtWhole.indexOf("0");
      var idx9_fmt = fmtWhole.indexOf("9");

      if (idx0_fmt < 0) {
        idx0_fmt = fmtWhole.length;
      }

      if (idx9_fmt < 0) {
        idx9_fmt = fmtWhole.length;
      }

      var idxStart = (idx0_fmt < idx9_fmt ? idx0_fmt : idx9_fmt);
      res += fmtWhole.substr(0, idxStart);

      fmtWhole = fmtWhole.substr(idxStart);

      var suffix = "";// 后缀
      if (fmtFractional.length == 0) {
        var idxLast0 = fmtWhole.lastIndexOf('0');
        var idxLast9 = fmtWhole.lastIndexOf('9');

        if (idxLast0 < 0) {
          idxLast0 = fmtWhole.length;
        }
        if (idxLast9 < 0) {
          idxLast9 = fmtWhole.length;
        }

        var idxLastNum = (idxLast0 > idxLast9 ? idxLast0 : idxLast9);// 最后一个非数字格式

        suffix = fmtWhole.substr(idxLastNum + 1);// 小数部分后缀

        fmtWhole = fmtWhole.substr(0, fmtWhole.length - suffix.length);

      }

      var whole = "";
      var len = strWhole.length;
      var pos = fmtWhole.length - 1;// 整数部分格式当前位 位置，右侧压栈
      var separator = _find_separator(fmtWhole);
      for (var i = 0; i < len; i++) {
        var char = strWhole.charAt(len - 1 - i);
        var f = fmtWhole.charAt(pos);
        while (!(f == '0' || f == '9')) {
          whole = f + whole;
          pos--;
          if (pos < 0) {
            whole = separator + whole;
            fmtWhole = fmtWhole.replace(/0/g, '9');
            pos = fmtWhole.length - 1;
          }
          f = fmtWhole.charAt(pos);
        }
        pos--;

        whole = char + whole;

      }

      fmtWhole = fmtWhole.substr(0, pos + 1).replace(/9/g, "");
      whole = fmtWhole + whole;
      if (whole.indexOf(separator) == 0) {
        whole = whole.substr(separator.length);
      }

      res += whole;
      // 处理整数部分结束
      // 处理小数部分
      if (fmtFractional.indexOf('.') == 0) {// 格式中包含小数点则处理小数部分，否则不处理.
        var idxLast0 = fmtFractional.lastIndexOf('0');
        var idxLast9 = fmtFractional.lastIndexOf('9');
        var idxLastNum = 0;
        if (idxLast0 < 0 && idxLast9 < 0) {
          idxLastNum = 1;
        } else if ((idxLast0 < 0 && idxLast9 >= 0)
            || (idxLast0 >= 0 && idxLast9 < 0)) {
          idxLastNum = Math.max(idxLast0, idxLast9) + 1;
        } else {
          if (idxLast0 < 0) {
            idxLast0 = fmtFractional.length;
          }
          if (idxLast9 < 0) {
            idxLast9 = fmtFractional.length;
          }

          idxLastNum = (idxLast0 > idxLast9 ? idxLast0 : idxLast9) + 1;// 最后一个非数字格式
        }

        if ((!suffix) || suffix == '') {
          suffix = fmtFractional.substr(idxLastNum);// 小数部分后缀
        }

        fmtFractional = fmtFractional.substr(0, idxLastNum);
        if (fmtFractional == '.') {
          fmtFractional = strFractional.replace(/./g, '9');
        } else {
          fmtFractional = fmtFractional.substr(1);
        }
        var separator = _find_separator(fmtFractional);

        var fractional = "";
        var len = strFractional.length;
        var pos = 0;// 小数部分格式当前位 位置，左侧压栈
        for (var i = 0; i < len; i++) {
          var char = strFractional.charAt(i);
          var f = fmtFractional.charAt(pos);
          while (!(f == '0' || f == '9')) {
            fractional = fractional + f;
            pos++;
            if (pos >= fmtFractional.length) {
              break;
            }
            f = fmtFractional.charAt(pos);
          }
          if (pos >= fmtFractional.length) {
            break;
          }
          pos++;

          fractional = fractional + char;

        }
        var sub = fmtFractional.substr(pos).replace(/9/g, "");
        fractional = fractional + sub;
        // alert(fractional+":"+suffix)
        if (fractional.lastIndexOf(separator) == fractional.length
            - separator.length) {
          fractional = fractional.substr(0, fractional
              .lastIndexOf(separator));
        }
        res += ('.' + fractional);
      }

      res = res + suffix;
      if(res[res.length-1]=="."){
        res=res.substr(0,res.length-1);
      }
      
      return res;
    };

    var NUMBER_UTILS = new NumberUtils();

    var interfaceFunc = function(options) {
      var args = [];
      for (var i = 1; i < arguments.length; i++) {
        args[args.length] = arguments[i];
      }
      return jq.numberUtils.METHODS[options].apply(NUMBER_UTILS, args);
    };

    interfaceFunc.METHODS = {
      format : function() {
        return NumberUtils.format.apply(NUMBER_UTILS, arguments);
      }
    };

    return interfaceFunc;
  })();
})